package com.zhicheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhicheng.exception.BaseException;
import com.zhicheng.mapper.UserMapper;
import com.zhicheng.pojo.Banner;
import com.zhicheng.pojo.User;
import com.zhicheng.pojo.dto.UserDTO;
import com.zhicheng.pojo.dto.UserLoginDTO;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.pojo.result.Result;
import com.zhicheng.service.UserService;
import com.zhicheng.utils.Md5Util;
import com.zhicheng.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册用户
     * @param userLoginDTO
     */
    @Override
    public void register(UserLoginDTO userLoginDTO) {

        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        //查询用户
        User user = userMapper.getByUsername(username);
        if (user != null) {
            //用户已存在
            throw new BaseException("用户已存在");
        } else {
            //注册
            //密码加密
            String md5String = Md5Util.getMD5String(password);
            //添加用户
            userMapper.add(username, md5String);
        }
    }


    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);
        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //用户不存在
            throw new BaseException("用户不存在");
        }
        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        if (!Md5Util.getMD5String(password).equals(user.getPassword())) {
            //密码错误
            throw new BaseException("密码错误");
        }

        if (user.getStatus() == 0) {
            //账号被锁定
            throw new BaseException("账号已被封禁，请联系管理员！！！");
        }

        //3、返回实体对象
        return user;

    }

    @Override
    public void update(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updatePwd(Map<String, String> params) {
        String old_pwd = params.get("old_pwd");
        String new_pwd = params.get("new_pwd");
        String re_pwd = params.get("re_pwd");

        Long userId = ThreadLocalUtil.getCurrentId();
        User loginUser = userMapper.getUserById(userId);
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(old_pwd))){
            throw new BaseException("原密码不正确");
        }
        if (!re_pwd.equals(new_pwd)){
            throw new BaseException("两次输入的密码不一致");
        }
        userMapper.updatePwd(Md5Util.getMD5String(new_pwd));
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Long userId = ThreadLocalUtil.getCurrentId();
        userMapper.updateAvatar(avatarUrl,userId);
    }

    @Override
    public PageResult<User> list(int page, int pageSize) {
        PageResult<User> pageResult = new PageResult<>();
        PageHelper.startPage(page,pageSize);
        List<User> userList = userMapper.list();
        Page<User> p = (Page<User>) userList;
        pageResult.setTotal(p.getTotal());
        pageResult.setItems(p.getResult());
        pageResult.setPages(p.getPages());
        pageResult.setPage(p.getPageNum());
        pageResult.setPageSize(p.getPageSize());
        return pageResult;
    }

    @Override
    public void updateUserStatus(User user) {
        user.setUpdateTime(LocalDateTime.now());
        User user1 = userMapper.getUserById(user.getId());
        user.setRoleId(user1.getRoleId());
        if (user.getRoleId() == 1){
            throw new BaseException("不能更改管理员用户的状态");
        }
        userMapper.update(user);
    }


}
